package com.qf.dao.impl;

import com.qf.dao.OrderDao;
import com.qf.domain.Order;
import com.qf.util.DataSourceUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * @author Administrator
 * @version v1.0
 * @date 2022/4/20
 */
public class OrderDaoImpl implements OrderDao {
    @Override
    public long findCountByAid(String id) {
        QueryRunner qf = new QueryRunner(DataSourceUtils.getDatasource());
        String sql = "select count(0) from tb_order where aid=?";
        try {
            long count  =(long) qf.query(sql, new ScalarHandler(), id);
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("查询订单数量失败!!");
        }
        return 0;
    }

    @Override
    public void insert(Order order) {
        Connection conn = DataSourceUtils.getConnection();
        QueryRunner qf = new QueryRunner();
        String sql = "INSERT INTO `tb_order` (`id`, `uid`, `money`, `status`, `time`, `aid`) VALUES (?, ?, ?, ?, ?, ?)";
        try {
            qf.update(conn, sql , order.getId(),order.getUid(), order.getMoney(), order.getStatus(), order.getTime(),order.getAid());
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("添加订单失败");
        }
    }
}
